package com.dyz.leetcode._05_sort;

public class _50_myPow {
    //快速幂的方法
    public double myPow(double x, int n) {
        long b = n;
        if(b<0){
            x = 1/x;
            b = -b;
        }
        return quickPow(x, b);
    }

    public double quickPow(double x, long n){
        if(n==0){return 1.0;}
        if(n==1){return x;}

        long mid = n/2;
        double y = quickPow(x, mid);
        return n % 2 == 0 ? y * y : x * y * y;
    }
}
